<template>
	<template>
	  <button type="primary" @click="A">通用OCR识别</button>
	   <text>识别内容：</text>
	  <view class="result-container">
	    <view class="result-item" v-for="(item, index) in items" :key="index">
	      <text>{{item.text || '未识别'}}</text>
	    </view>
	  </view>
	</template>
	
</template>

<script>
export default {
	data() {
			return {
				items: []
			}
		},
	methods: {
		A() {
			this.photo("A")
		},
		photo(type) {
			let that = this
			wx.chooseMedia({
				count: 1,
				mediaType: ['image', 'video'],
				sourceType: ['album', 'camera'],
				success(res) {
					// tempFilePath可以作为img标签的src属性显示图片
					let imgUrl = res.tempFiles[0].tempFilePath;
					console.log("res.tempFiles[0].tempFilePath" + res.tempFiles[0].tempFilePath)
					console.log("res.tempFiles[0].size)" + res.tempFiles[0].size)
					that.uploadImg(type, imgUrl)
				}
			})
		},
		uploadImg(type, imgUrl) {
			let that = this
			wx.cloud.uploadFile({
				cloudPath: 'ocr/' + type + '.png',
				filePath: imgUrl, // 文件路径
				success: res => {
					console.log("上传成功", res.fileID)
					that.getImgUrl(type, res.fileID)
				},
				fail: err => {
					console.log("上传失败", err)
				}
			})
		},
		getImgUrl(type, imgUrl) {
			let that = this
			wx.cloud.getTempFileURL({
				fileList: [imgUrl],
				success: res => {
					let imgUrl = res.fileList[0].tempFileURL
					console.log("获取图片url成功:", imgUrl + "获取图片type成功:" + type)
					that.shibie(type, imgUrl)
				},
				fail: err => {
					console.log("获取图片url失败", err)
				}
			})
		},
		shibie(type, imgUrl) {
			wx.cloud.callFunction({
				name: "quickstartFunctions",
				data: {
					type: type,
					imgUrl: imgUrl
				},
				success: (res) => { 
					this.items = res.result.items
				},
				fail(res) {
					console.log("识别失败", res)
				}
			})
		}
		
		
	}
	}
</script>

<style>
</style>